package com.hwxm.ltim.controller;

import com.hwxm.ltim.common.Result;
import com.hwxm.ltim.dto.LoginRequest;
import com.hwxm.ltim.dto.LoginResponse;
import com.hwxm.ltim.dto.RegisterRequest;
import com.hwxm.ltim.dto.EmailRegisterRequest;
import com.hwxm.ltim.dto.SendVerificationCodeRequest;
import com.hwxm.ltim.dto.VerifyCodeRequest;
import com.hwxm.ltim.dto.ResetPasswordRequest;
import com.hwxm.ltim.entity.User;
import com.hwxm.ltim.service.UserService;
import com.hwxm.ltim.service.EmailVerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmailVerificationCodeService emailVerificationCodeService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<User> register(@RequestBody RegisterRequest request) {
        try {
            User user = userService.register(request);
            return Result.success("注册成功", user);
        } catch (Exception e) {
            log.error("注册失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody LoginRequest request) {
        try {
            LoginResponse response = userService.login(request);
            return Result.success("登录成功", response);
        } catch (Exception e) {
            log.error("登录失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/send-verification-code")
    public Result<String> sendVerificationCode(@RequestBody SendVerificationCodeRequest request) {
        try {
            emailVerificationCodeService.sendVerificationCode(request.getEmail(), request.getType());
            return Result.success("验证码发送成功");
        } catch (Exception e) {
            log.error("发送验证码失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 验证邮箱验证码
     */
    @PostMapping("/verify-code")
    public Result<String> verifyCode(@RequestBody VerifyCodeRequest request) {
        try {
            boolean isValid = emailVerificationCodeService.verifyCode(request.getEmail(), request.getCode(), request.getType());
            if (isValid) {
                return Result.success("验证码验证成功");
            } else {
                return Result.error("验证码错误或已过期");
            }
        } catch (Exception e) {
            log.error("验证码验证失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 邮箱注册
     */
    @PostMapping("/email-register")
    public Result<User> emailRegister(@RequestBody EmailRegisterRequest request) {
        try {
            User user = userService.emailRegister(request);
            return Result.success("注册成功", user);
        } catch (Exception e) {
            log.error("邮箱注册失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    public Result<String> resetPassword(@RequestBody ResetPasswordRequest request) {
        try {
            userService.resetPassword(request);
            return Result.success("密码重置成功");
        } catch (Exception e) {
            log.error("密码重置失败", e);
            return Result.error(e.getMessage());
        }
    }
}

